install.packages("caret",repos = "http://cran.us.r-project.org")
## package 'caret' successfully unpacked and MD5 sums checked
## 
## The downloaded binary packages are in
##  C:\Users\mjone\AppData\Local\Temp\RtmpwD0YEm\downloaded_packages
pacman::p_load(pacman,janitor,readr,gmodels,tidyr,dplyr,ggthemes,gapminder,plotly,cowplot,tmap,ggplot2,RColorBrewer,gganimate,gifski,sf,factoextra,e1071,cluster,kernlab,corrplot,corrgram,hrbrthemes,viridis,glmnet,rpart,rpart.plot,partykit,maps,tmap,ggmap,htmltools,animation) 

Introduction

For our final project we will be reviewing Economic Freedom across the worls. With data ranging from 1970-2016.

Topics Covered: * Exploratory Analysis (Summaries and Filtered Data Frames) * Exploratory Plots * Linear Regression * Variable Selection: Lasso Method * Clustering

Step 1: We start by reading in and cleaning the data. In total we have 3726 observations

freedom <- read_csv("C:\\Users\\mjone\\OneDrive\\Desktop\\MBA 8045\\Data\\efw_cc.csv") # Update to your file location
freedom <- freedom %>%
  clean_names()

Now that we have access to all of our data we will take a look at it to see which variables we have access to

str(freedom)#3726 Observations
## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 3726 obs. of  36 variables:
##  $ year                               : num  2016 2016 2016 2016 2016 ...
##  $ iso_code                           : chr  "ALB" "DZA" "AGO" "ARG" ...
##  $ countries                          : chr  "Albania" "Algeria" "Angola" "Argentina" ...
##  $ economic_freedom                   : num  7.54 4.99 5.17 4.84 7.57 7.98 7.58 6.49 7.34 7.56 ...
##  $ rank                               : num  34 159 155 160 29 10 27 106 49 30 ...
##  $ quartile                           : num  1 4 4 4 1 1 1 3 2 1 ...
##  $ x1a_government_consumption         : num  8.23 2.15 7.6 5.34 7.26 ...
##  $ x1b_transfers                      : num  7.51 7.82 8.89 6.05 7.75 ...
##  $ x1c_gov_enterprises                : num  8 0 0 6 8 10 10 0 7 10 ...
##  $ x1d_top_marg_tax_rate              : num  8 4.5 9.5 4 5 5 3.5 6.5 10 10 ...
##  $ x1_size_government                 : num  7.94 3.62 6.5 5.35 7 ...
##  $ x2a_judicial_independence          : num  2.67 4.19 1.84 3.69 3.87 ...
##  $ x2b_impartial_courts               : num  3.15 4.33 1.97 2.93 4.2 ...
##  $ x2c_protection_property_rights     : num  4.51 4.69 2.51 4.26 5.66 ...
##  $ x2d_military_interference          : num  8.33 4.17 3.33 7.5 5.83 ...
##  $ x2e_integrity_legal_system         : num  4.17 5 4.17 3.33 5 ...
##  $ x2f_legal_enforcement_contracts    : num  4.39 4.51 2.3 3.63 5.2 ...
##  $ x2g_restrictions_sale_real_property: num  6.49 6.63 5.46 6.86 9.8 ...
##  $ x2h_reliability_police             : num  6.93 6.14 3.02 3.39 5.71 ...
##  $ x2i_business_costs_crime           : num  6.22 6.74 4.29 4.13 7.01 ...
##  $ x2j_gender_adjustment              : num  0.949 0.821 0.846 0.769 1 ...
##  $ x2_property_rights                 : num  5.07 4.69 2.96 3.9 5.81 ...
##  $ x3a_money_growth                   : num  8.99 6.96 9.39 5.23 9.08 ...
##  $ x3b_std_inflation                  : num  9.48 8.34 4.99 5.22 9.26 ...
##  $ x3c_inflation                      : num  9.74 8.72 3.05 2 9.75 ...
##  $ x3d_freedom_own_foreign_currency   : num  10 5 5 10 10 10 10 5 0 10 ...
##  $ x3_sound_money                     : num  9.55 7.25 5.61 5.61 9.52 ...
##  $ x4a_tariffs                        : num  8.96 6.87 6.99 6.42 8.55 ...
##  $ x4b_regulatory_trade_barriers      : num  7.49 2.48 2.02 4.81 7.19 ...
##  $ x4c_black_market                   : num  10 5.56 10 0 10 ...
##  $ x4d_control_movement_capital_ppl   : num  6.41 1.59 2.04 4.7 6.83 ...
##  $ x4_trade                           : num  8.21 4.13 5.26 3.98 8.14 ...
##  $ x5a_credit_market_reg              : num  7.1 5.1 7.06 5.42 9.1 ...
##  $ x5b_labor_market_reg               : num  6.92 5.03 4.56 5.15 6.23 ...
##  $ x5c_business_reg                   : num  6.71 5.68 4.93 5.54 6.8 ...
##  $ x5_regulation                      : num  6.91 5.27 5.52 5.37 7.38 ...
##  - attr(*, "spec")=
##   .. cols(
##   ..   year = col_double(),
##   ..   ISO_code = col_character(),
##   ..   countries = col_character(),
##   ..   `ECONOMIC FREEDOM` = col_double(),
##   ..   rank = col_double(),
##   ..   quartile = col_double(),
##   ..   `1a_government_consumption` = col_double(),
##   ..   `1b_transfers` = col_double(),
##   ..   `1c_gov_enterprises` = col_double(),
##   ..   `1d_top_marg_tax_rate` = col_double(),
##   ..   `1_size_government` = col_double(),
##   ..   `2a_judicial_independence` = col_double(),
##   ..   `2b_impartial_courts` = col_double(),
##   ..   `2c_protection_property_rights` = col_double(),
##   ..   `2d_military_interference` = col_double(),
##   ..   `2e_integrity_legal_system` = col_double(),
##   ..   `2f_legal_enforcement_contracts` = col_double(),
##   ..   `2g_restrictions_sale_real_property` = col_double(),
##   ..   `2h_reliability_police` = col_double(),
##   ..   `2i_business_costs_crime` = col_double(),
##   ..   `2j_gender_adjustment` = col_double(),
##   ..   `2_property_rights` = col_double(),
##   ..   `3a_money_growth` = col_double(),
##   ..   `3b_std_inflation` = col_double(),
##   ..   `3c_inflation` = col_double(),
##   ..   `3d_freedom_own_foreign_currency` = col_double(),
##   ..   `3_sound_money` = col_double(),
##   ..   `4a_tariffs` = col_double(),
##   ..   `4b_regulatory_trade_barriers` = col_double(),
##   ..   `4c_black_market` = col_double(),
##   ..   `4d_control_movement_capital_ppl` = col_double(),
##   ..   `4_trade` = col_double(),
##   ..   `5a_credit_market_reg` = col_double(),
##   ..   `5b_labor_market_reg` = col_double(),
##   ..   `5c_business_reg` = col_double(),
##   ..   `5_regulation` = col_double()
##   .. )
summary(freedom)
##       year        iso_code          countries         economic_freedom
##  Min.   :1970   Length:3726        Length:3726        Min.   :1.970   
##  1st Qu.:1995   Class :character   Class :character   1st Qu.:5.855   
##  Median :2005   Mode  :character   Mode  :character   Median :6.680   
##  Mean   :2001                                         Mean   :6.520   
##  3rd Qu.:2011                                         3rd Qu.:7.350   
##  Max.   :2016                                         Max.   :9.190   
##                                                       NA's   :723     
##       rank           quartile     x1a_government_consumption x1b_transfers   
##  Min.   :  1.00   Min.   :1.000   Min.   : 0.000             Min.   : 0.000  
##  1st Qu.: 33.00   1st Qu.:1.000   1st Qu.: 4.450             1st Qu.: 6.208  
##  Median : 66.00   Median :3.000   Median : 6.082             Median : 8.432  
##  Mean   : 68.31   Mean   :2.498   Mean   : 5.862             Mean   : 7.673  
##  3rd Qu.:102.00   3rd Qu.:3.000   3rd Qu.: 7.571             3rd Qu.: 9.482  
##  Max.   :162.00   Max.   :4.000   Max.   :10.000             Max.   :10.000  
##  NA's   :723      NA's   :723     NA's   :589                NA's   :960     
##  x1c_gov_enterprises x1d_top_marg_tax_rate x1_size_government
##  Min.   : 0.000      Min.   : 0.000        Min.   :0.6538    
##  1st Qu.: 4.000      1st Qu.: 4.000        1st Qu.:5.2294    
##  Median : 7.000      Median : 6.000        Median :6.3197    
##  Mean   : 5.738      Mean   : 5.813        Mean   :6.2321    
##  3rd Qu.: 8.000      3rd Qu.: 8.000        3rd Qu.:7.2677    
##  Max.   :10.000      Max.   :10.000        Max.   :9.9047    
##  NA's   :646         NA's   :1047          NA's   :647       
##  x2a_judicial_independence x2b_impartial_courts x2c_protection_property_rights
##  Min.   :0.000             Min.   :0.000        Min.   : 0.000                
##  1st Qu.:3.259             1st Qu.:3.352        1st Qu.: 4.093                
##  Median :4.698             Median :4.333        Median : 5.299                
##  Mean   :5.019             Mean   :4.675        Mean   : 5.487                
##  3rd Qu.:6.823             3rd Qu.:5.857        3rd Qu.: 7.058                
##  Max.   :9.817             Max.   :9.686        Max.   :10.000                
##  NA's   :1553              NA's   :1179         NA's   :1129                  
##  x2d_military_interference x2e_integrity_legal_system
##  Min.   : 0.000            Min.   : 0.000            
##  1st Qu.: 4.500            1st Qu.: 4.167            
##  Median : 6.667            Median : 6.667            
##  Mean   : 6.510            Mean   : 6.200            
##  3rd Qu.: 8.333            3rd Qu.: 8.333            
##  Max.   :10.000            Max.   :10.000            
##  NA's   :1186              NA's   :1153              
##  x2f_legal_enforcement_contracts x2g_restrictions_sale_real_property
##  Min.   :0.000                   Min.   :0.000                      
##  1st Qu.:3.376                   1st Qu.:6.001                      
##  Median :4.492                   Median :7.528                      
##  Mean   :4.449                   Mean   :7.045                      
##  3rd Qu.:5.583                   3rd Qu.:8.523                      
##  Max.   :8.479                   Max.   :9.981                      
##  NA's   :1361                    NA's   :1384                       
##  x2h_reliability_police x2i_business_costs_crime x2j_gender_adjustment
##  Min.   :0.000          Min.   :0.000            Min.   :0.0000       
##  1st Qu.:4.064          1st Qu.:4.641            1st Qu.:0.7931       
##  Median :5.321          Median :6.055            Median :0.9564       
##  Mean   :5.505          Mean   :5.904            Mean   :0.8732       
##  3rd Qu.:7.111          3rd Qu.:7.315            3rd Qu.:1.0000       
##  Max.   :9.688          Max.   :9.673            Max.   :1.0000       
##  NA's   :2071           NA's   :2071             NA's   :66           
##  x2_property_rights x3a_money_growth x3b_std_inflation x3c_inflation   
##  Min.   :0.9556     Min.   :0.000    Min.   :0.000     Min.   : 0.000  
##  1st Qu.:3.9439     1st Qu.:7.867    1st Qu.:7.555     1st Qu.: 8.192  
##  Median :5.2306     Median :8.697    Median :8.881     Median : 9.106  
##  Mean   :5.2529     Mean   :8.235    Mean   :8.008     Mean   : 8.464  
##  3rd Qu.:6.3925     3rd Qu.:9.256    3rd Qu.:9.404     3rd Qu.: 9.574  
##  Max.   :9.2783     Max.   :9.999    Max.   :9.952     Max.   :10.000  
##  NA's   :755        NA's   :644      NA's   :607       NA's   :607     
##  x3d_freedom_own_foreign_currency x3_sound_money   x4a_tariffs    
##  Min.   : 0.00                    Min.   :0.000   Min.   : 0.000  
##  1st Qu.: 0.00                    1st Qu.:6.604   1st Qu.: 6.209  
##  Median : 5.00                    Median :7.969   Median : 7.480  
##  Mean   : 5.92                    Mean   :7.661   Mean   : 7.079  
##  3rd Qu.:10.00                    3rd Qu.:9.257   3rd Qu.: 8.334  
##  Max.   :10.00                    Max.   :9.922   Max.   :10.000  
##  NA's   :621                      NA's   :611     NA's   :681     
##  x4b_regulatory_trade_barriers x4c_black_market
##  Min.   :0.000                 Min.   : 0.000  
##  1st Qu.:5.463                 1st Qu.:10.000  
##  Median :6.592                 Median :10.000  
##  Mean   :6.333                 Mean   : 9.115  
##  3rd Qu.:7.516                 3rd Qu.:10.000  
##  Max.   :9.833                 Max.   :10.000  
##  NA's   :1348                  NA's   :617     
##  x4d_control_movement_capital_ppl    x4_trade      x5a_credit_market_reg
##  Min.   : 0.000                   Min.   : 0.000   Min.   : 0.000       
##  1st Qu.: 2.000                   1st Qu.: 5.947   1st Qu.: 6.800       
##  Median : 4.769                   Median : 7.011   Median : 8.306       
##  Mean   : 4.376                   Mean   : 6.720   Mean   : 7.732       
##  3rd Qu.: 6.493                   3rd Qu.: 7.916   3rd Qu.: 9.333       
##  Max.   :10.000                   Max.   :10.000   Max.   :10.000       
##  NA's   :592                      NA's   :691      NA's   :619          
##  x5b_labor_market_reg x5c_business_reg x5_regulation  
##  Min.   :1.837        Min.   :2.010    Min.   :1.002  
##  1st Qu.:5.073        1st Qu.:5.387    1st Qu.:5.878  
##  Median :6.197        Median :6.170    Median :6.759  
##  Mean   :6.168        Mean   :6.189    Mean   :6.638  
##  3rd Qu.:7.344        3rd Qu.:7.007    3rd Qu.:7.486  
##  Max.   :9.725        Max.   :9.504    Max.   :9.440  
##  NA's   :1158         NA's   :1402     NA's   :728

Variable Description:

Area 1:

  • Size of Government—As government spending, taxation, and the size of government-controlled enterprises increase, government decision-making is substituted for individual choice and economic freedom is reduced.

Area 2:

  • Legal System and Property Rights—Protection of persons and their rightfully acquired property is a central element of both economic freedom and civil society. Indeed, it is the most important function of government.

Area 3:

  • Sound Money—Inflation erodes the value of rightfully earned wages and savings. Sound money is thus essential to protect property rights. When inflation is not only high but also volatile, it becomes difficult for individuals to plan for the future and thus use economic freedom effectively.

Area 4:

  • Freedom to Trade Internationally—Freedom to exchange—in its broadest sense, buying, selling, making contracts, and so on—is essential to economic freedom, which is reduced when freedom to exchange does not include businesses and individuals in other nations.

Area 5:

  • Regulation—Governments not only use a number of tools to limit the right to exchange internationally, they may also develop onerous regulations that limit the right to exchange, gain credit, hire or work for whom you wish, or freely operate your business.

Creating Data Frame for 2016

Next we will create a data frame for 2016 our most recent year. From there we will do some plotting to get a better view of what has occured over time.

freedom2016 <- filter(freedom, year == "2016") #Filtering to 2016 most recent year in data
freedom2016 <- freedom2016[order(freedom2016$rank),]#Sorting data frame from lowest to highest
head(freedom2016)
## # A tibble: 6 x 36
##    year iso_code countries economic_freedom  rank quartile x1a_government_~
##   <dbl> <chr>    <chr>                <dbl> <dbl>    <dbl>            <dbl>
## 1  2016 HKG      Hong Kong             8.97     1        1             7.92
## 2  2016 SGP      Singapore             8.84     2        1             4.82
## 3  2016 NZL      New Zeal~             8.49     3        1             4.62
## 4  2016 CHE      Switzerl~             8.39     4        1             6.4 
## 5  2016 IRL      Ireland               8.07     5        1             3.74
## 6  2016 USA      United S~             8.03     6        1             6.71
## # ... with 29 more variables: x1b_transfers <dbl>, x1c_gov_enterprises <dbl>,
## #   x1d_top_marg_tax_rate <dbl>, x1_size_government <dbl>,
## #   x2a_judicial_independence <dbl>, x2b_impartial_courts <dbl>,
## #   x2c_protection_property_rights <dbl>, x2d_military_interference <dbl>,
## #   x2e_integrity_legal_system <dbl>, x2f_legal_enforcement_contracts <dbl>,
## #   x2g_restrictions_sale_real_property <dbl>, x2h_reliability_police <dbl>,
## #   x2i_business_costs_crime <dbl>, x2j_gender_adjustment <dbl>,
## #   x2_property_rights <dbl>, x3a_money_growth <dbl>, x3b_std_inflation <dbl>,
## #   x3c_inflation <dbl>, x3d_freedom_own_foreign_currency <dbl>,
## #   x3_sound_money <dbl>, x4a_tariffs <dbl>,
## #   x4b_regulatory_trade_barriers <dbl>, x4c_black_market <dbl>,
## #   x4d_control_movement_capital_ppl <dbl>, x4_trade <dbl>,
## #   x5a_credit_market_reg <dbl>, x5b_labor_market_reg <dbl>,
## #   x5c_business_reg <dbl>, x5_regulation <dbl>
summary(freedom2016)
##       year        iso_code          countries         economic_freedom
##  Min.   :2016   Length:162         Length:162         Min.   :2.880   
##  1st Qu.:2016   Class :character   Class :character   1st Qu.:6.260   
##  Median :2016   Mode  :character   Mode  :character   Median :6.905   
##  Mean   :2016                                         Mean   :6.795   
##  3rd Qu.:2016                                         3rd Qu.:7.468   
##  Max.   :2016                                         Max.   :8.970   
##                                                                       
##       rank           quartile     x1a_government_consumption x1b_transfers   
##  Min.   :  1.00   Min.   :1.000   Min.   : 0.000             Min.   : 2.010  
##  1st Qu.: 41.25   1st Qu.:2.000   1st Qu.: 4.122             1st Qu.: 6.295  
##  Median : 81.50   Median :2.500   Median : 5.665             Median : 8.135  
##  Mean   : 81.27   Mean   :2.506   Mean   : 5.597             Mean   : 7.646  
##  3rd Qu.:121.75   3rd Qu.:3.750   3rd Qu.: 7.329             3rd Qu.: 9.461  
##  Max.   :162.00   Max.   :4.000   Max.   :10.000             Max.   :10.000  
##                                                              NA's   :11      
##  x1c_gov_enterprises x1d_top_marg_tax_rate x1_size_government
##  Min.   : 0.000      Min.   : 1.000        Min.   :3.617     
##  1st Qu.: 4.000      1st Qu.: 5.000        1st Qu.:5.661     
##  Median : 7.000      Median : 6.750        Median :6.532     
##  Mean   : 6.287      Mean   : 6.519        Mean   :6.484     
##  3rd Qu.: 8.000      3rd Qu.: 8.375        3rd Qu.:7.410     
##  Max.   :10.000      Max.   :10.000        Max.   :9.528     
##  NA's   :5                                                   
##  x2a_judicial_independence x2b_impartial_courts x2c_protection_property_rights
##  Min.   :0.2168            Min.   :0.6413       Min.   :1.149                 
##  1st Qu.:3.4160            1st Qu.:3.0405       1st Qu.:4.529                 
##  Median :4.5898            Median :4.0325       Median :5.445                 
##  Mean   :4.9706            Mean   :4.2046       Mean   :5.635                 
##  3rd Qu.:6.4326            3rd Qu.:5.2664       3rd Qu.:6.527                 
##  Max.   :9.6975            Max.   :7.9810       Max.   :9.288                 
##  NA's   :10                                     NA's   :10                    
##  x2d_military_interference x2e_integrity_legal_system
##  Min.   : 0.000            Min.   : 1.667            
##  1st Qu.: 4.167            1st Qu.: 4.167            
##  Median : 6.667            Median : 5.833            
##  Mean   : 6.345            Mean   : 6.058            
##  3rd Qu.: 8.333            3rd Qu.: 8.333            
##  Max.   :10.000            Max.   :10.000            
##                            NA's   :25                
##  x2f_legal_enforcement_contracts x2g_restrictions_sale_real_property
##  Min.   :0.000                   Min.   :2.009                      
##  1st Qu.:3.376                   1st Qu.:6.615                      
##  Median :4.353                   Median :7.778                      
##  Mean   :4.310                   Mean   :7.526                      
##  3rd Qu.:5.339                   3rd Qu.:8.695                      
##  Max.   :7.661                   Max.   :9.981                      
##                                  NA's   :2                          
##  x2h_reliability_police x2i_business_costs_crime x2j_gender_adjustment
##  Min.   :1.396          Min.   :0.7782           Min.   :0.4146       
##  1st Qu.:4.210          1st Qu.:4.6760           1st Qu.:0.7866       
##  Median :5.572          Median :5.7466           Median :0.9231       
##  Mean   :5.697          Mean   :5.7212           Mean   :0.8658       
##  3rd Qu.:7.432          3rd Qu.:6.9157           3rd Qu.:1.0000       
##  Max.   :9.688          Max.   :9.0224           Max.   :1.0000       
##  NA's   :10             NA's   :10                                    
##  x2_property_rights x3a_money_growth x3b_std_inflation x3c_inflation   
##  Min.   :1.856      Min.   :2.768    Min.   :0.000     Min.   : 0.000  
##  1st Qu.:4.003      1st Qu.:8.074    1st Qu.:8.408     1st Qu.: 8.813  
##  Median :5.100      Median :8.783    Median :9.262     Median : 9.623  
##  Mean   :5.214      Mean   :8.596    Mean   :8.563     Mean   : 8.990  
##  3rd Qu.:6.069      3rd Qu.:9.360    3rd Qu.:9.584     3rd Qu.: 9.842  
##  Max.   :8.798      Max.   :9.985    Max.   :9.899     Max.   :10.000  
##                                                                        
##  x3d_freedom_own_foreign_currency x3_sound_money   x4a_tariffs    
##  Min.   : 0.000                   Min.   :1.942   Min.   : 3.253  
##  1st Qu.: 5.000                   1st Qu.:7.204   1st Qu.: 6.873  
##  Median :10.000                   Median :8.642   Median : 7.748  
##  Mean   : 6.821                   Mean   :8.242   Mean   : 7.489  
##  3rd Qu.:10.000                   3rd Qu.:9.397   3rd Qu.: 8.266  
##  Max.   :10.000                   Max.   :9.922   Max.   :10.000  
##                                                   NA's   :1       
##  x4b_regulatory_trade_barriers x4c_black_market
##  Min.   :0.000                 Min.   : 0.000  
##  1st Qu.:4.330                 1st Qu.:10.000  
##  Median :5.928                 Median :10.000  
##  Mean   :5.813                 Mean   : 9.688  
##  3rd Qu.:7.597                 3rd Qu.:10.000  
##  Max.   :9.729                 Max.   :10.000  
##                                NA's   :1       
##  x4d_control_movement_capital_ppl    x4_trade     x5a_credit_market_reg
##  Min.   :1.154                    Min.   :2.877   Min.   : 3.333       
##  1st Qu.:3.907                    1st Qu.:6.357   1st Qu.: 7.410       
##  Median :5.341                    Median :7.169   Median : 8.596       
##  Mean   :5.115                    Mean   :7.018   Mean   : 8.219       
##  3rd Qu.:6.619                    3rd Qu.:7.952   3rd Qu.: 9.386       
##  Max.   :9.233                    Max.   :9.324   Max.   :10.000       
##                                                                        
##  x5b_labor_market_reg x5c_business_reg x5_regulation  
##  Min.   :2.107        Min.   :2.010    Min.   :2.484  
##  1st Qu.:5.328        1st Qu.:5.799    1st Qu.:6.475  
##  Median :6.470        Median :6.385    Median :7.060  
##  Mean   :6.376        Mean   :6.454    Mean   :7.016  
##  3rd Qu.:7.367        3rd Qu.:7.246    3rd Qu.:7.714  
##  Max.   :9.373        Max.   :9.273    Max.   :9.440  
## 
#options(repr.plot.width=8, repr.plot.height=10)

Visualize our Data

The below chart has each country in our data set and their score over time 1-10. We can see from our plots that some countries were not established in certain periods. This means we will have to deal with NA values in our analysis. Keep this in mind.

#Plot Below Covers All Countries and Year to get a view of changes
freedom %>% filter(!is.na(economic_freedom) | !is.na(year)) %>% group_by(year) %>%
  ggplot(aes(year,economic_freedom, fill = economic_freedom, group = 1))+
  geom_line(aes(color = economic_freedom))+
  scale_color_viridis(option = "plasma",direction = -1, discrete=FALSE) +
  facet_wrap(~countries)+theme_bw()+
  theme(legend.position = "none", axis.text.x = element_blank(), 
        strip.text.x = element_text(size = 6))+
  xlab(" ") + ylab("")+ ggtitle("ECONOMIC FREEDOM OF THE WORLD 1970-2016")
## Warning: Removed 4 rows containing missing values (geom_path).

Scatter Plots

The next three plots show the relation of x (Trade, Sound Money, and Regulations) to economic freedom. The size of the bubble represents the rank. Larger bubbles indicates a higher rank.

Please note a separate file will be send for the video
#Yearly Scatter Plot for US, Potentially other countries
p <- ggplot(freedom, aes(x4_trade, economic_freedom, color = countries)) +
  geom_point(aes(size = -rank, frame = year, ids = countries)) +
  scale_x_log10()
p <- ggplotly(p)
p
p4 <- ggplot(freedom, aes(x3_sound_money, economic_freedom, color = countries)) +
  geom_point(aes(size = -rank, frame = year, ids = countries)) +
  scale_x_log10()
p4 <- ggplotly(p4)
p4
p5 <- ggplot(freedom, aes(x5c_business_reg, economic_freedom, color = countries)) +
  geom_point(aes(size = -rank, frame = year, ids = countries)) +
  scale_x_log10()
p5 <- ggplotly(p5)
p5

Selected Countries Plot

After reviewing our initial line graphs we selected a few countries to show 6 countries trends. Our x-axis is time and y-axis is Economic Freedom

# Keep only 3 names
country <- filter(freedom,countries %in% c("United States", "China", "Venezuela", "United Kingdom", "Ghana","Zimbabwe"))
 
# Plot
country %>%
  ggplot( aes(x=year, y=economic_freedom, group=countries, color=countries)) +
    geom_line() +
    geom_point() +
    scale_color_viridis(discrete = TRUE) +
    ggtitle("Countries Performance Over Time") +
    theme_ipsum() +
    ylab("Economic Freedom") +
    transition_reveal(year)

# Save at gif:
anim_save("Economic Freedom by Year.gif")

Economic Freedom Bubble Plot Here we can see the distribution of our

##WORK IN PROGRESS NEED TO UPDATE

#static 2016 Scatter Plot
a1 <- ggplotly(ggplot(freedom2016, aes(quartile,economic_freedom , size = -rank)) + 
                 geom_jitter(aes(color=countries, alpha=0.5)) +
                 theme_bw()+ theme(legend.position= "none")+
                 xlab("Quartile") + 
                 ggtitle("Economic Freedom Index 2016"), tooltip = c("countries"))
a1

2016 Maps (Rank and Trade)

From both of these visualizations we can see that trade and economic_freedom are visually similar to rank. The differences do indicate the other variables should be considered when trying to increase the rank profile of a country.

jBrewColors <- brewer.pal(n = 8, name = "BuPu")


p1 <- plot_geo(freedom2016) %>%
  add_trace(z = ~-rank, color = ~-rank,colors = jBrewColors,
            text = ~freedom2016$countries, locations = ~freedom2016$iso_code) %>%
  colorbar(title = 'Countries' , tickprefix = 'Rank') %>%
  layout(title = 'Economic Freedom 2016 by Rank') 

p1
p2 <- plot_geo(freedom2016) %>%
  add_trace(z = ~x5c_business_reg, color = ~x5c_business_reg,colors = jBrewColors,
            text = ~freedom2016$countries, locations = ~freedom2016$iso_code) %>%
  colorbar(title = 'Countries' , tickprefix = 'Trade') %>%
  layout(title = 'Economic Freedom 2016 by Trade') 

p2
p8 <- plot_geo(freedom2016) %>%
  add_trace(z = ~economic_freedom, color = ~-rank,colors = jBrewColors,
            text = ~freedom2016$countries, locations = ~freedom2016$iso_code) %>%
  colorbar(title = 'Countries' , tickprefix = 'Rank') %>%
  layout(title = 'Economic Freedom 2016 by Rank') 

p8

Assigning Clusters For 2016

fviz_nbclust(na.omit(freedom2016[4:36]), kmeans, method = "silhouette")

gap_stat <- clusGap(na.omit(freedom2016[4:36]), FUN = kmeans, nstart = 25,
                    K.max = 10, B = 50)
fviz_gap_stat(gap_stat)

n_cluster <- 4

results  <- kmeans(na.omit(freedom2016[4:36]),n_cluster)
results
## K-means clustering with 4 clusters of sizes 26, 34, 32, 29
## 
## Cluster means:
##   economic_freedom      rank quartile x1a_government_consumption x1b_transfers
## 1         6.701923  92.53846 2.807692                   5.643794      7.660434
## 2         7.827353  18.85294 1.000000                   4.848356      6.391209
## 3         5.793438 135.87500 3.812500                   6.300299      8.580960
## 4         7.292759  53.82759 1.931034                   5.677600      7.186032
##   x1c_gov_enterprises x1d_top_marg_tax_rate x1_size_government
## 1            6.000000              6.923077           6.556826
## 2            8.676471              5.941176           6.464303
## 3            4.468750              5.984375           6.333596
## 4            7.448276              6.137931           6.612460
##   x2a_judicial_independence x2b_impartial_courts x2c_protection_property_rights
## 1                  4.531053             3.992858                       5.283979
## 2                  6.897442             5.333483                       7.279879
## 3                  3.502248             3.442479                       4.446322
## 4                  5.166858             4.198570                       5.889236
##   x2d_military_interference x2e_integrity_legal_system
## 1                  6.025641                   5.544872
## 2                  8.774510                   7.990196
## 3                  4.375000                   4.791667
## 4                  7.270115                   6.063218
##   x2f_legal_enforcement_contracts x2g_restrictions_sale_real_property
## 1                        4.447582                            7.508096
## 2                        5.321731                            8.311222
## 3                        3.265367                            6.470558
## 4                        4.532511                            7.809283
##   x2h_reliability_police x2i_business_costs_crime x2j_gender_adjustment
## 1               5.067634                 5.386412             0.8680815
## 2               7.567842                 6.920993             0.9669095
## 3               4.507362                 4.774881             0.7815347
## 4               5.703705                 5.593773             0.9143553
##   x2_property_rights x3a_money_growth x3b_std_inflation x3c_inflation
## 1           4.922193         8.625213          8.740109      8.919356
## 2           7.048105         8.780676          9.541676      9.776266
## 3           3.906946         8.556711          7.586815      7.543010
## 4           5.535438         8.668712          9.216567      9.465114
##   x3d_freedom_own_foreign_currency x3_sound_money x4a_tariffs
## 1                         6.346154       8.157708    7.391465
## 2                        10.000000       9.524654    8.224081
## 3                         3.906250       6.898196    6.813615
## 4                         9.655172       9.251391    7.827085
##   x4b_regulatory_trade_barriers x4c_black_market
## 1                      5.682171         9.982093
## 2                      7.880651        10.000000
## 3                      4.061851         8.989404
## 4                      6.552729        10.000000
##   x4d_control_movement_capital_ppl x4_trade x5a_credit_market_reg
## 1                         4.955593 7.002831              8.061171
## 2                         6.771267 8.219000              9.178555
## 3                         3.791417 5.914072              6.914557
## 4                         6.230559 7.652593              8.815506
##   x5b_labor_market_reg x5c_business_reg x5_regulation
## 1             6.225815         6.316754      6.867913
## 2             6.872708         7.586695      7.879319
## 3             5.572057         5.261378      5.915998
## 4             6.770012         6.650672      7.412063
## 
## Clustering vector:
##   [1] 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 4 4 4
##  [38] 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 1 1 1 1 1 1 1 1 1 1 1
##  [75] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3
## [112] 3 3 3 3 3 3 3 3 3 3
## 
## Within cluster sum of squares by cluster:
## [1] 5140.445 5007.579 9203.978 4697.174
##  (between_SS / total_SS =  91.2 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
freedom2016 <- na.omit(freedom2016)
freedom2016$cluster_number <- results$cluster

Assigning Clusters for All Years

fviz_nbclust(na.omit(freedom[4:36]), kmeans, method = "silhouette")

gap_stat <- clusGap(na.omit(freedom[4:36]), FUN = kmeans, nstart = 25,
                    K.max = 10, B = 50)
fviz_gap_stat(gap_stat)

n_cluster <- 4

results2  <- kmeans(na.omit(freedom[4:36]),n_cluster)
results2
## K-means clustering with 4 clusters of sizes 385, 259, 314, 358
## 
## Cluster means:
##   economic_freedom      rank quartile x1a_government_consumption x1b_transfers
## 1         7.822883  17.76364 1.002597                   4.794888      6.327232
## 2         5.726757 130.15830 3.857143                   6.486306      8.503878
## 3         6.625605  90.08280 2.917197                   5.914559      7.845109
## 4         7.226285  53.06425 1.930168                   5.600535      7.195419
##   x1c_gov_enterprises x1d_top_marg_tax_rate x1_size_government
## 1            8.550649              5.902597           6.393842
## 2            4.146718              5.918919           6.263955
## 3            6.305732              6.761146           6.706637
## 4            7.617318              6.449721           6.715748
##   x2a_judicial_independence x2b_impartial_courts x2c_protection_property_rights
## 1                  7.106385             6.126717                       7.596226
## 2                  3.177699             3.367870                       4.208899
## 3                  4.391869             4.158129                       5.249542
## 4                  4.903138             4.306861                       5.795554
##   x2d_military_interference x2e_integrity_legal_system
## 1                  8.807899                   8.218625
## 2                  4.299073                   4.930022
## 3                  6.034143                   5.656171
## 4                  7.692988                   6.162228
##   x2f_legal_enforcement_contracts x2g_restrictions_sale_real_property
## 1                        5.816585                            8.384423
## 2                        3.613266                            6.326609
## 3                        4.392753                            7.317927
## 4                        4.557858                            7.642644
##   x2h_reliability_police x2i_business_costs_crime x2j_gender_adjustment
## 1               7.533995                 7.269152             0.9652184
## 2               4.123892                 4.915084             0.8238479
## 3               4.746553                 5.458198             0.9028994
## 4               5.364499                 5.732524             0.9220318
##   x2_property_rights x3a_money_growth x3b_std_inflation x3c_inflation
## 1           7.308987         8.917465          9.227359      9.486486
## 2           3.943435         7.922469          7.231142      7.818829
## 3           4.997103         8.346155          8.465144      8.748987
## 4           5.555794         8.616402          8.737107      9.047933
##   x3d_freedom_own_foreign_currency x3_sound_money x4a_tariffs
## 1                         9.974026       9.401334    8.136390
## 2                         3.030888       6.500832    6.712690
## 3                         5.589172       7.787364    7.155248
## 4                         9.511173       8.978154    7.866255
##   x4b_regulatory_trade_barriers x4c_black_market
## 1                      7.826377        10.000000
## 2                      4.766509         8.970826
## 3                      6.130137         9.900482
## 4                      6.566989        10.000000
##   x4d_control_movement_capital_ppl x4_trade x5a_credit_market_reg
## 1                         6.610393 8.143290              9.212191
## 2                         3.474387 5.981103              7.212481
## 3                         4.568715 6.938646              8.118631
## 4                         6.010345 7.610897              8.771564
##   x5b_labor_market_reg x5c_business_reg x5_regulation
## 1             6.903759         7.485554      7.867168
## 2             5.594121         5.031458      5.946020
## 3             6.022475         5.952039      6.697715
## 4             6.665918         6.375392      7.270958
## 
## Clustering vector:
##    [1] 1 2 2 2 1 1 1 3 1 2 4 2 4 2 4 2 2 1 1 3 3 2 1 2 3 1 1 1 4 2 2 4 1 2 1 4 2
##   [38] 4 1 3 3 1 3 4 1 4 4 3 4 2 1 4 4 4 4 4 4 4 1 3 1 3 3 1 1 2 2 3 2 1 3 3 4 2
##   [75] 2 2 1 1 4 2 1 3 2 1 2 3 4 4 4 4 1 3 2 3 2 1 4 4 3 1 3 2 4 1 2 1 3 3 3 2 3
##  [112] 4 2 4 1 1 4 2 2 3 2 1 2 2 2 1 1 1 2 4 2 4 2 4 2 4 2 2 1 1 2 3 2 4 2 3 1 4
##  [149] 1 4 2 2 4 1 2 1 4 2 4 1 3 2 1 3 4 1 4 4 3 3 1 4 4 4 4 4 4 4 1 3 1 3 1 1 2
##  [186] 2 4 2 1 3 3 4 2 2 3 1 1 4 2 1 3 2 1 2 3 4 4 4 1 1 3 2 3 2 1 4 4 3 1 3 3 1
##  [223] 1 2 1 3 3 3 2 3 4 2 4 1 1 4 2 2 3 2 4 2 2 2 1 1 1 2 4 2 1 2 4 2 4 2 2 1 1
##  [260] 2 3 4 2 3 4 4 1 4 2 2 4 1 2 1 4 2 4 1 2 3 1 3 4 1 4 4 2 3 1 4 4 4 1 1 4 4
##  [297] 1 3 1 3 1 1 2 2 3 2 1 3 3 4 2 2 3 1 1 4 2 1 3 2 4 2 3 4 4 4 1 1 3 2 3 2 1
##  [334] 4 3 3 4 3 3 1 1 2 1 3 3 3 3 3 4 2 1 1 1 4 2 3 3 2 4 2 2 2 1 1 1 2 4 2 4 3
##  [371] 4 2 4 2 2 1 1 2 3 2 1 2 3 4 4 1 4 2 2 4 1 2 1 4 2 4 1 3 3 1 2 4 1 4 3 2 3
##  [408] 1 4 4 4 1 1 4 4 4 3 1 3 1 1 3 2 4 2 1 3 3 4 2 2 3 1 1 4 2 1 3 2 4 3 3 1 1
##  [445] 4 1 1 3 2 3 2 1 4 3 4 4 3 3 1 1 2 1 3 3 3 2 3 4 2 1 1 1 4 2 2 3 2 4 2 2 2
##  [482] 1 1 1 2 4 2 4 3 4 2 4 2 2 1 1 2 3 1 2 3 1 4 1 4 2 2 4 1 2 1 4 2 3 1 3 3 4
##  [519] 2 4 1 4 3 3 3 2 1 4 4 4 1 1 4 4 1 3 1 3 1 1 3 2 3 2 1 3 3 4 2 2 3 1 1 4 2
##  [556] 1 4 2 4 3 1 4 4 4 1 3 2 3 2 1 4 3 3 4 3 1 1 2 1 3 3 3 3 4 4 2 1 1 1 4 2 2
##  [593] 4 2 4 2 2 2 1 1 1 2 1 2 4 2 4 3 4 2 2 1 1 2 3 1 2 3 1 4 1 4 2 2 4 1 2 1 4
##  [630] 2 4 1 3 3 4 2 4 1 4 3 3 3 2 1 4 4 4 1 1 3 3 1 3 4 4 1 1 2 3 3 2 1 3 3 4 3
##  [667] 2 3 1 1 4 2 1 3 2 4 3 1 4 4 4 4 3 2 3 2 1 1 3 4 1 3 1 1 2 1 3 3 3 3 4 4 2
##  [704] 1 1 1 4 2 2 4 2 4 2 2 1 1 1 2 1 3 1 3 4 3 4 2 1 1 2 3 4 2 3 1 4 1 4 2 3 4
##  [741] 1 2 1 1 1 3 3 4 2 4 1 4 3 3 3 2 1 4 4 4 1 1 3 3 1 4 4 4 1 1 3 2 3 3 3 4 3
##  [778] 2 3 1 1 4 2 1 4 2 4 3 1 4 4 4 4 3 2 3 1 1 3 4 4 3 1 1 2 1 3 3 3 3 3 1 2 1
##  [815] 1 1 4 2 4 2 4 2 2 1 1 1 2 4 3 4 3 3 3 4 2 1 1 3 3 1 2 3 1 4 1 4 2 3 1 1 2
##  [852] 1 1 1 3 4 4 4 1 4 4 3 3 2 1 4 4 4 1 4 4 4 4 4 4 1 1 3 3 3 3 3 4 3 4 1 1 4
##  [889] 2 1 4 3 4 3 1 3 4 4 1 3 2 3 1 1 4 3 4 3 1 1 2 1 3 3 3 3 3 4 2 1 1 1 4 2 4
##  [926] 2 4 2 1 1 1 3 1 3 4 3 4 3 4 2 1 1 3 3 4 2 3 1 4 1 3 2 3 1 1 2 1 1 1 3 4 4
##  [963] 4 1 4 4 3 3 1 4 4 4 1 4 4 3 1 4 1 1 1 3 3 3 3 3 3 4 1 1 4 2 1 4 2 4 3 1 3
## [1000] 4 4 4 3 2 3 1 1 4 3 4 3 1 1 2 1 3 3 3 4 4 2 1 1 1 4 2 4 2 4 3 4 1 1 2 4 2
## [1037] 4 3 4 3 4 2 1 1 3 3 1 2 3 1 4 1 3 2 3 1 1 2 1 4 1 3 4 4 4 1 4 1 3 3 1 4 4
## [1074] 4 1 4 4 4 1 4 1 1 1 2 3 3 3 4 3 4 1 1 4 3 1 4 2 1 3 4 3 3 4 1 3 2 3 1 1 4
## [1111] 4 1 3 4 1 2 1 3 3 3 3 4 2 1 1 1 4 2 4 2 4 2 4 1 1 3 4 3 4 3 4 3 4 2 1 1 3
## [1148] 3 1 3 4 1 3 2 3 1 1 2 1 4 1 4 4 4 1 4 1 3 3 1 4 4 1 1 4 4 4 1 4 1 1 1 2 3
## [1185] 3 3 4 3 3 1 1 3 1 4 3 3 4 4 4 4 4 2 1 1 4 4 1 3 1 1 1 3 3 3 3 4 2 1 1 1 4
## [1222] 2 4 2 4 3 4 1 1 3 3 1 3 4 3 4 2 1 1 3 3 1 3 4 1 3 2 3 1 1 2 1 4 1 4 4 4 1
## [1259] 4 1 3 3 1 4 4 1 1 4 4 4 1 4 1 4 1 2 3 3 3 3 1 1 4 1 3 1 3 4 4 4 4 4 3 3 1
## [1296] 1 4 4 1 3 1 1 1 3 4 3 3 4 2 4 1 1 4 2 3 2
## 
## Within cluster sum of squares by cluster:
## [1] 57728.68 67854.57 62085.51 60041.22
##  (between_SS / total_SS =  90.1 %)
## 
## Available components:
## 
## [1] "cluster"      "centers"      "totss"        "withinss"     "tot.withinss"
## [6] "betweenss"    "size"         "iter"         "ifault"
freedom <- na.omit(freedom)
freedom$cluster_number <- results2$cluster

Cluster Plots 2016

Note to Megan correct the color to be specific to each cluster not scale

fviz_cluster(results, data = na.omit(freedom2016[4:36]))

jBrewColors2 <- brewer.pal(n = 8, name = "Spectral")
p3 <- plot_geo(freedom2016) %>%
  add_trace(z = ~cluster_number, color = ~cluster_number,colors = jBrewColors2,
            text = ~freedom2016$countries, locations = ~freedom2016$iso_code) %>%
  colorbar(title = 'Countries' , tickprefix = 'Clusters') %>%
  layout(title = 'Economic Freedom 2016 by Cluster') 

p3

# Cluster Plots All Years Note to Megan correct the color to be specific to each cluster not scale. Will only include if can get animation to work

Animated Map For Cluster

Chunck Above trying to solve the same problem

Regression

Now we get into the meat of our analysis. We start be prepping our data for regression. Gathering some test and training data

str(freedom)
## Classes 'tbl_df', 'tbl' and 'data.frame':    1316 obs. of  37 variables:
##  $ year                               : num  2016 2016 2016 2016 2016 ...
##  $ iso_code                           : chr  "ALB" "DZA" "AGO" "ARG" ...
##  $ countries                          : chr  "Albania" "Algeria" "Angola" "Argentina" ...
##  $ economic_freedom                   : num  7.54 4.99 5.17 4.84 7.57 7.98 7.58 6.49 7.56 6.3 ...
##  $ rank                               : num  34 159 155 160 29 10 27 106 30 120 ...
##  $ quartile                           : num  1 4 4 4 1 1 1 3 1 3 ...
##  $ x1a_government_consumption         : num  8.23 2.15 7.6 5.34 7.26 ...
##  $ x1b_transfers                      : num  7.51 7.82 8.89 6.05 7.75 ...
##  $ x1c_gov_enterprises                : num  8 0 0 6 8 10 10 0 10 7 ...
##  $ x1d_top_marg_tax_rate              : num  8 4.5 9.5 4 5 5 3.5 6.5 10 8 ...
##  $ x1_size_government                 : num  7.94 3.62 6.5 5.35 7 ...
##  $ x2a_judicial_independence          : num  2.67 4.19 1.84 3.69 3.87 ...
##  $ x2b_impartial_courts               : num  3.15 4.33 1.97 2.93 4.2 ...
##  $ x2c_protection_property_rights     : num  4.51 4.69 2.51 4.26 5.66 ...
##  $ x2d_military_interference          : num  8.33 4.17 3.33 7.5 5.83 ...
##  $ x2e_integrity_legal_system         : num  4.17 5 4.17 3.33 5 ...
##  $ x2f_legal_enforcement_contracts    : num  4.39 4.51 2.3 3.63 5.2 ...
##  $ x2g_restrictions_sale_real_property: num  6.49 6.63 5.46 6.86 9.8 ...
##  $ x2h_reliability_police             : num  6.93 6.14 3.02 3.39 5.71 ...
##  $ x2i_business_costs_crime           : num  6.22 6.74 4.29 4.13 7.01 ...
##  $ x2j_gender_adjustment              : num  0.949 0.821 0.846 0.769 1 ...
##  $ x2_property_rights                 : num  5.07 4.69 2.96 3.9 5.81 ...
##  $ x3a_money_growth                   : num  8.99 6.96 9.39 5.23 9.08 ...
##  $ x3b_std_inflation                  : num  9.48 8.34 4.99 5.22 9.26 ...
##  $ x3c_inflation                      : num  9.74 8.72 3.05 2 9.75 ...
##  $ x3d_freedom_own_foreign_currency   : num  10 5 5 10 10 10 10 5 10 0 ...
##  $ x3_sound_money                     : num  9.55 7.25 5.61 5.61 9.52 ...
##  $ x4a_tariffs                        : num  8.96 6.87 6.99 6.42 8.55 ...
##  $ x4b_regulatory_trade_barriers      : num  7.49 2.48 2.02 4.81 7.19 ...
##  $ x4c_black_market                   : num  10 5.56 10 0 10 ...
##  $ x4d_control_movement_capital_ppl   : num  6.41 1.59 2.04 4.7 6.83 ...
##  $ x4_trade                           : num  8.21 4.13 5.26 3.98 8.14 ...
##  $ x5a_credit_market_reg              : num  7.1 5.1 7.06 5.42 9.1 ...
##  $ x5b_labor_market_reg               : num  6.92 5.03 4.56 5.15 6.23 ...
##  $ x5c_business_reg                   : num  6.71 5.68 4.93 5.54 6.8 ...
##  $ x5_regulation                      : num  6.91 5.27 5.52 5.37 7.38 ...
##  $ cluster_number                     : int  1 2 2 2 1 1 1 3 1 2 ...
##  - attr(*, "na.action")= 'omit' Named int  9 12 13 15 16 17 19 22 25 26 ...
##   ..- attr(*, "names")= chr  "9" "12" "13" "15" ...
freedom$high_ef <- ifelse(freedom$economic_freedom > 6, 1, 0) # Converting streams to binary data 
freedom <- na.omit(freedom)
index_training <- sample(dim(freedom)[1], 40, replace = FALSE) # replace = false will prevent code from selecting same observation twice
training_data  <- freedom[index_training, ]
test_data      <- freedom[-index_training, ]

Since we dont want to randomly select variables to use we will use the Lasso Method for variable selection

typeof(training_data[, 7:36])
## [1] "list"
X_training <- data.matrix(training_data[, 7:36])
X_test     <- data.matrix(test_data[, 7:36])

model_linear_lasso <- cv.glmnet(X_training, training_data$economic_freedom, family="gaussian", nfolds = 10, type.measure = "mse", alpha = 1)
summary(model_linear_lasso)
##            Length Class  Mode     
## lambda     42     -none- numeric  
## cvm        42     -none- numeric  
## cvsd       42     -none- numeric  
## cvup       42     -none- numeric  
## cvlo       42     -none- numeric  
## nzero      42     -none- numeric  
## call        7     -none- call     
## name        1     -none- character
## glmnet.fit 12     elnet  list     
## lambda.min  1     -none- numeric  
## lambda.1se  1     -none- numeric
model_linear_lasso$lambda
##  [1] 0.65772163 0.59929145 0.54605205 0.49754229 0.45334200 0.41306835
##  [7] 0.37637250 0.34293660 0.31247106 0.28471199 0.25941896 0.23637290
## [13] 0.21537418 0.19624093 0.17880743 0.16292267 0.14844907 0.13526126
## [19] 0.12324502 0.11229628 0.10232018 0.09323034 0.08494801 0.07740147
## [25] 0.07052533 0.06426006 0.05855137 0.05334983 0.04861038 0.04429197
## [31] 0.04035719 0.03677197 0.03350525 0.03052873 0.02781665 0.02534549
## [37] 0.02309387 0.02104227 0.01917293 0.01746966 0.01591771 0.01450362
coef(model_linear_lasso, s = 0.4)
## 31 x 1 sparse Matrix of class "dgCMatrix"
##                                              1
## (Intercept)                         4.56304805
## x1a_government_consumption          .         
## x1b_transfers                       .         
## x1c_gov_enterprises                 .         
## x1d_top_marg_tax_rate               .         
## x1_size_government                  .         
## x2a_judicial_independence           .         
## x2b_impartial_courts                .         
## x2c_protection_property_rights      .         
## x2d_military_interference           .         
## x2e_integrity_legal_system          .         
## x2f_legal_enforcement_contracts     .         
## x2g_restrictions_sale_real_property .         
## x2h_reliability_police              .         
## x2i_business_costs_crime            .         
## x2j_gender_adjustment               .         
## x2_property_rights                  0.06484596
## x3a_money_growth                    .         
## x3b_std_inflation                   .         
## x3c_inflation                       .         
## x3d_freedom_own_foreign_currency    .         
## x3_sound_money                      .         
## x4a_tariffs                         .         
## x4b_regulatory_trade_barriers       .         
## x4c_black_market                    .         
## x4d_control_movement_capital_ppl    .         
## x4_trade                            0.18000302
## x5a_credit_market_reg               .         
## x5b_labor_market_reg                .         
## x5c_business_reg                    .         
## x5_regulation                       0.09948984
coef(model_linear_lasso, s = 500)
## 31 x 1 sparse Matrix of class "dgCMatrix"
##                                          1
## (Intercept)                         6.9485
## x1a_government_consumption          .     
## x1b_transfers                       .     
## x1c_gov_enterprises                 .     
## x1d_top_marg_tax_rate               .     
## x1_size_government                  .     
## x2a_judicial_independence           .     
## x2b_impartial_courts                .     
## x2c_protection_property_rights      .     
## x2d_military_interference           .     
## x2e_integrity_legal_system          .     
## x2f_legal_enforcement_contracts     .     
## x2g_restrictions_sale_real_property .     
## x2h_reliability_police              .     
## x2i_business_costs_crime            .     
## x2j_gender_adjustment               .     
## x2_property_rights                  .     
## x3a_money_growth                    .     
## x3b_std_inflation                   .     
## x3c_inflation                       .     
## x3d_freedom_own_foreign_currency    .     
## x3_sound_money                      .     
## x4a_tariffs                         .     
## x4b_regulatory_trade_barriers       .     
## x4c_black_market                    .     
## x4d_control_movement_capital_ppl    .     
## x4_trade                            .     
## x5a_credit_market_reg               .     
## x5b_labor_market_reg                .     
## x5c_business_reg                    .     
## x5_regulation                       .
as.double(coef(model_linear_lasso, s = 0.4))
##  [1] 4.56304805 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
##  [7] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
## [13] 0.00000000 0.00000000 0.00000000 0.00000000 0.06484596 0.00000000
## [19] 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000 0.00000000
## [25] 0.00000000 0.00000000 0.18000302 0.00000000 0.00000000 0.00000000
## [31] 0.09948984
as.double(coef(model_linear_lasso, s = 500))
##  [1] 6.9485 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## [11] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## [21] 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000 0.0000
## [31] 0.0000
#After Lasso Linear Regression
mlr2 <- lm(economic_freedom ~ x3_sound_money + x4_trade + x5c_business_reg, data = training_data)
anova(mlr2)
## Analysis of Variance Table
## 
## Response: economic_freedom
##                  Df  Sum Sq Mean Sq F value    Pr(>F)    
## x3_sound_money    1 12.4509 12.4509 139.149 6.309e-14 ***
## x4_trade          1  5.8537  5.8537  65.420 1.298e-09 ***
## x5c_business_reg  1  4.6241  4.6241  51.678 1.859e-08 ***
## Residuals        36  3.2212  0.0895                      
## ---
## Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Now that we have our predicted values for our model we want to check how our work.

Provide insights by cluster and prediction points

Finally we plot our linear regression with the variables selected from the variable selection

plot_grid(ggplot(freedom, aes(x3_sound_money, economic_freedom)) + 
            geom_point(color='#50C878', alpha = 0.5)+
            xlab("Sound Money") + ylab("Economic Freedom ")+
            geom_smooth(method = lm)+theme_bw(),
          ggplot(freedom, aes(x4_trade, economic_freedom)) + 
            geom_point(color='#50C878', alpha = 0.5)+
            xlab("Trade") + ylab("")+
            geom_smooth(method = lm)+theme_bw(),
          ggplot(freedom, aes(x5c_business_reg, economic_freedom)) + 
            geom_point(color='#50C878', alpha = 0.5)+
            xlab("Business_Regulations") + ylab("")+
            geom_smooth(method = lm)+theme_bw(),align = 'h')

Conclusion/ Key Takeaways

Note to Group: We can certainly change these as everyone becomes familiar with the data and forms their own takeaways

Dealing with a long time frame created the challenge of dealing with null values.

Cluster 2 had the key attributes of countries that ranked the highests.

Since the aspects of Economic Freedom have been broken down into measurable variables we were able to build sound models

Our models proved to be significant in predicting economic freedom or rank (update this later). If government officials want to improve the lives of their citizens through economic freedom they should focus on: * Having sound money * Free Trade * Business Regulation